//
//  HLYiYanLabel.m
//  Various
//
//  Created by HarwordLiu on 16/3/28.
//  Copyright © 2016年 Harword. All rights reserved.
//

#import "HLYiYanLabel.h"

@implementation HLYiYanLabel

- (void)drawRect:(CGRect)rect {
    // Drawing code
    float labelHeight = self.frame.size.height;
    float labelWidth = self.frame.size.width;
    
    float startX = 0;
    float startY = 0;
    float width = 25 * sHEIGHT;
    float height = 20 * sHEIGHT;
    float x;
    float y;
    
    NSInteger charNumber;
    NSInteger containerNumber;
    containerNumber = floor(labelWidth / 25 * sHEIGHT);
    
    charNumber = floor(labelHeight / 20 * sHEIGHT);

    NSString *drawStr = self.attributedText.string;

    NSInteger lineNumber = ceil([drawStr length] / charNumber);
    
    startX = labelWidth / 2 + ((lineNumber - 1) * 25 / 2) * sHEIGHT;
    
    if (lineNumber >= containerNumber) {
        NSRange range = NSMakeRange(0, containerNumber * containerNumber - 1);
        drawStr = [drawStr substringWithRange:range];
        drawStr = [drawStr stringByAppendingFormat:@"..."];
    }
    for (int i = 0; i < [drawStr length]; i++) {
        x = startX - floor(i / charNumber) * width;
        y = startY + (i % charNumber) * height;
        CGRect Aframe = CGRectMake(x, y, width, height);
        NSRange range = NSMakeRange(i, 1);
        NSString *str = [drawStr substringWithRange:range];
        
        NSDictionary* attrs =@{NSFontAttributeName : [UIFont fontWithName:@"FZQKBYSJW--GB1-0" size:18 * sHEIGHT]};
        [str drawInRect:Aframe withAttributes:attrs];
        
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
